<template>
  <div>
    <el-dialog v-loading="isLoading" title="分配权限" :visible="isVisible" width="50%">
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        @close="$emit('update:isVisible', false)"
      >
        <el-form-item label="角色选择">
          <el-checkbox-group v-model="form.authlist">
            <!-- <el-checkbox v-for="item in authArr" :key="item.id" :label="item.name" name="authlist" /> -->
            <el-checkbox
              v-for="item in authArr"
              :key="item.id"
              :label="item.id"
              name="authlist"
            >{{ item.name }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('update:isVisible',false)">取 消</el-button>
        <el-button type="primary" @click="submitlist">确 定1</el-button>
        <el-button type="primary" @click="submitlist2">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
// eslint-disable-next-line no-unused-vars
import { getPermissionList } from '@/api/auth'
import { assignPrem, getRoleInfo } from '@/api/role'
export default {
  name: 'DistributAuth',
  components: {},
  props: {
    isVisible: {
      type: Boolean,
      required: true
    },
    roleId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      isLoading: true,
      roleInfo: {},
      authArr: [],
      form: {
        authlist: []
      }
    }
  },
  computed: {},
  watch: {
    isVisible(newVal, oldVal) {
      if (newVal === false) return
      // 当newVal为true才能往下执行，代表弹窗显示
      this.getRoleInfo()
    }
  },
  created() {
    this.getPermissionList()
  },
  mounted() {},
  methods: {
    // 获取权限数据
    async getPermissionList() {
      this.isLoading = true
      const { data } = await getPermissionList()
      this.authArr = data
      this.isLoading = false
      // console.log(1111)
      // console.log(data)
    },
    async getRoleInfo() {
      this.isLoading = true
      const { data } = await getRoleInfo(this.roleId)
      this.roleInfo = data
      this.form.authList = data.permIds
      this.isLoading = false
    },
    submitlist() {
      console.log(22222)
      console.log(this.authArr)
      console.log(this.form.authlist)
      // map 映射
      const arr = this.form.authlist.map(name => {
        const a = this.authArr.find(obj => obj.name === name)
        return a.id
      })
      // const arr = this.form.authList.map(name => this.authArr.find(obj => obj.name === name).id)

      // 上传数据
      assignPrem({
        id: this.roleId,
        permIds: arr
      })
    },
    async submitlist2() {
      // 上传数据
      assignPrem({
        id: this.roleId,
        permIds: this.form.authlist
      })
      this.$message.success('成功')
      this.$emit('update:isVisible', false)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
